package com.xunqi.mall.controller;

import com.github.pagehelper.PageInfo;
import com.xunqi.mall.consts.MallConst;
import com.xunqi.mall.enums.ResponseEnum;
import com.xunqi.mall.service.OrderService;
import com.xunqi.mall.vo.OrderVo;
import com.xunqi.mall.vo.ResponseVo;
import com.xunqi.mall.vo.UserLoginVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-05-11 15:56
 **/

@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping(value = "/orders")
    public ResponseVo<OrderVo> addOrder(@RequestParam("shippingId") Integer shippingId,
                                        HttpServletRequest request) {

        if (StringUtils.isEmpty(shippingId)) {
            return ResponseVo.error(ResponseEnum.PARAM_ERROR);
        }

        //从session中获取用户的值
        UserLoginVo userLoginVo = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<OrderVo> orderVoResponseVo = orderService.addOrder(userLoginVo.getId(), shippingId);

        return orderVoResponseVo;
    }


    @GetMapping(value = "/orders")
    public ResponseVo orderList(@RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize,
                                @RequestParam(value = "pageNum",defaultValue = "10") Integer pageNum,
                                HttpServletRequest request) {

        //从session中获取用户的值
        UserLoginVo userLoginVo = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<PageInfo> pageInfoResponseVo = orderService.orderList(userLoginVo.getId(), pageSize, pageNum);

        return pageInfoResponseVo;
    }


    @GetMapping(value = "/orders/{orderNo}")
    public ResponseVo orderDetail(@PathVariable("orderNo") Long orderNo,
                                  HttpServletRequest request) {

        if (StringUtils.isEmpty(orderNo)) {
            return ResponseVo.error(ResponseEnum.PARAM_ERROR);
        }

        //从session中获取用户的值
        UserLoginVo userLoginVo = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo<OrderVo> orderVoResponseVo = orderService.detailOrder(userLoginVo.getId(), orderNo);

        return orderVoResponseVo;

    }

    @PutMapping(value = "/orders/{orderNo}")
    public ResponseVo cancelOrder(@PathVariable("orderNo") Long orderNo,
                                  HttpServletRequest request) {

        if (StringUtils.isEmpty(orderNo)) {
            return ResponseVo.error(ResponseEnum.PARAM_ERROR);
        }

        //从session中获取用户的值
        UserLoginVo userLoginVo = (UserLoginVo) request.getSession().getAttribute(MallConst.CURRENT_USER);

        ResponseVo responseVo = orderService.cancelOrder(userLoginVo.getId(), orderNo);

        return responseVo;

    }

}
